//
//  SAConnectedApi.m
//  IntroductionApp
//
//  Created by 英司 菅澤 on 11/12/14.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "SAConnectedApi.h"
#import "JSONKit.h"

@implementation SAConnectedApi

@synthesize Delegate = _Delegate;
@synthesize requestType = _requestType;
// version管理
@synthesize isSound = _isSound;
@synthesize isPhoto = _isPhoto;


-(void)dealloc{
    [ super dealloc ];
}

/* ====================================================================================== */
#pragma mark - request method
/* ====================================================================================== */

// バージョンAPI開始
-(void)getAppVersion {
    
    self.isSound = NO;
    self.isPhoto = NO;
    
    NSURL *url = [ NSURL URLWithString:SoundAPI_URL ];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.delegate = self;
    [request setDefaultResponseEncoding:NSUTF8StringEncoding];
    
    self.requestType = connectTypeVersion;
    [ request startAsynchronous ];
    
}

// newsApiを使用
-(void)getNewsAPI{
    
    NSURL *url = [ NSURL URLWithString:NewsAPI_URL ];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.delegate = self;
    [request setDefaultResponseEncoding:NSUTF8StringEncoding];
    
    self.requestType = connectTypeNews;
    [ request startAsynchronous ];
    
}

// newsplistを取得する
-(void)getNewsFile {
    
    NSString *urlStr = [ NSString stringWithFormat:@"%@/%@",NewsPlistURL,NewsFile ];
    
    NSURL *url = [NSURL URLWithString:urlStr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.delegate = self;
    
    self.requestType = connectTypeNews;
    // Basic認証
    [request setUsername:serverID];
    [request setPassword:serverPASS];
    
    [ request startAsynchronous ];
}


-(void)getMusicFileList{
    
    NSURL *url = [NSURL URLWithString:MusicFileList_API];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.delegate = self;
    [request setUsername:serverID];
    [request setPassword:serverPASS];
    self.requestType = connectTypeSound;
    [ request startAsynchronous ];
    
}

/* ====================================================================================== */
#pragma mark - youtube
/* ====================================================================================== */

// youtubeのデータを取得
-(void)getYouTubeData{
    
    NSURL *url = [NSURL URLWithString:YOUTUBE_URL];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setRequestMethod:@"GET"];
    request.delegate = self;
    
    self.requestType = connectTypeYoutube;
    
    [ request startAsynchronous ];

}

/* ====================================================================================== */
#pragma mark - finish method
/* ====================================================================================== */

- (void)requestFinished:(ASIHTTPRequest *)request{
    
    // delegate
    [ self.Delegate requestFinished:self:[ request responseString ]];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{    
    // delegate
    [ self.Delegate requestFailed:self:[ request error ]];

}


@end
